package com.share.rules.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.share.rules.config.DroolsHelper;
import com.share.rules.domain.*;
import com.share.rules.domain.vo.FeeRuleRequest;
import com.share.rules.domain.FeeRuleResponseVo;
import com.share.rules.domain.vo.FeeRuleResponse;
import com.share.rules.mapper.FeeRuleMapper;
import com.share.rules.service.IFeeRuleService;
import lombok.extern.slf4j.Slf4j;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Slf4j
@Service
public class FeeRuleServiceImpl extends ServiceImpl<FeeRuleMapper, FeeRule> implements IFeeRuleService
{
    @Autowired
    private FeeRuleMapper feeRuleMapper;


    @Override
    public List<FeeRule> selectFeeRuleList(FeeRule feeRule)
    {
        return feeRuleMapper.selectFeeRuleList(feeRule);
    }

    @Override
    public List<FeeRule> getALLFeeRuleList() {
        return feeRuleMapper.selectList(new LambdaQueryWrapper<FeeRule>().eq(FeeRule::getStatus, "1"));
    }
    @Autowired
    private KieContainer kieContainer;

    @Override
    public FeeRuleResponseVo calculateOrderFee(FeeRuleRequestForm feeRuleRequestForm) {

        // 1️⃣ 封装传入对象
        FeeRuleRequest feeRuleRequest = new FeeRuleRequest();
        feeRuleRequest.setDurations(feeRuleRequestForm.getDuration());
        log.warn(feeRuleRequestForm.getDuration().toString());
        log.info("传入参数：{}", JSON.toJSONString(feeRuleRequest));

        // 2️⃣ 获取最新订单费用规则（假设规则内容存储在数据库中）
        FeeRule feeRule = feeRuleMapper.selectById(feeRuleRequestForm.getFeeRuleId());

        // 3️⃣ 动态加载规则
        KieSession kieSession = DroolsHelper.loadForRule(feeRule.getRule());
        log.warn(kieSession.toString());
        log.info("DRL 内容:\n{}", feeRule.getRule());

        // 4️⃣ 封装返回对象
        FeeRuleResponse feeRuleResponse = new FeeRuleResponse();
        kieSession.setGlobal("feeRuleResponse", feeRuleResponse);

        // 5️⃣ 插入 fact
        kieSession.insert(feeRuleRequest);

        // 6️⃣ 触发规则
        int firedRules = kieSession.fireAllRules();
        log.info("触发了 {} 条规则", firedRules);

        // 7️⃣ 结束会话
        kieSession.dispose();

        // 8️⃣ 封装返回对象
        FeeRuleResponseVo feeRuleResponseVo = new FeeRuleResponseVo();
        feeRuleResponseVo.setTotalAmount(BigDecimal.valueOf(feeRuleResponse.getTotalAmount()));
        feeRuleResponseVo.setFreePrice(BigDecimal.valueOf(feeRuleResponse.getFreePrice()));
        feeRuleResponseVo.setExceedPrice(BigDecimal.valueOf(feeRuleResponse.getExceedPrice()));
        feeRuleResponseVo.setFreeDescription(feeRuleResponse.getFreeDescription());
        feeRuleResponseVo.setExceedDescription(feeRuleResponse.getExceedDescription());

        log.info("计算结果：{}", JSON.toJSONString(feeRuleResponseVo));
        return feeRuleResponseVo;
    }


}